home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / X / cursor / pixmapCursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  10.8 KB  |  301 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * pixmapCursor.c:
  19.  *
  20.  * This demonstrates how in X to define a cursor for one's own application
  21.  * without changing the cursor for the rest of the system.  In X11, this 
  22.  * can be done using either Pixmaps, Fonts, or Glyphs.  Study especially
  23.  * the functions:
  24.  *
  25.  *     XCreateFontCursor, XCreatePixmapCursor, XCreateGlyphCursor
  26.  *     XDefineCursor, XUndefineCursor
  27.  * 
  28.  * See /usr/include/X11/cursorfont.h for some predefined Font Cursors.
  29.  * 
  30.  * These change the cursor only for the Window they're defined for.
  31.  * The following is an example using Pixmap cursors.
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <X11/Xlib.h>
  36. #include <X11/Xutil.h>
  37.  
  38. #define xhair15_width 15
  39. #define xhair15_height 15
  40. #define xhair15_x_hot 7
  41. #define xhair15_y_hot 7
  42. static char xhair15_bits[] = {
  43.    0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
  44.    0x00, 0x00, 0x3f, 0x7e, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
  45.    0x80, 0x00, 0x80, 0x00, 0x80, 0x00};
  46.  
  47. #define xhair_width 16
  48. #define xhair_height 16
  49. #define xhair_x_hot 7
  50. #define xhair_y_hot 7
  51. static char xhair_bits[] = {
  52.    0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
  53.    0x00, 0x00, 0x3f, 0xfc, 0x3f, 0xfc, 0x00, 0x00, 0x80, 0x01, 0x80, 0x01,
  54.    0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01};
  55.  
  56. #define xhair17_width 17
  57. #define xhair17_height 17
  58. #define xhair17_x_hot 8
  59. #define xhair17_y_hot 8
  60. static char xhair17_bits[] = {
  61.    0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,
  62.    0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  63.    0x7f, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,
  64.    0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00,
  65.    0x00, 0x01, 0x00};
  66. #define xhair30_width 30
  67. #define xhair30_height 30
  68. #define xhair30_x_hot 16
  69. #define xhair30_y_hot 13
  70. static char xhair30_bits[] = {
  71.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  72.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  73.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  74.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  75.    0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x3f, 0xfe, 0x3f,
  76.    0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  77.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  78.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  79.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  80.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00};
  81.  
  82. #define xhair31_width 31
  83. #define xhair31_height 31
  84. #define xhair31_x_hot 15
  85. #define xhair31_y_hot 15
  86. static char xhair31_bits[] = {
  87.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  88.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  89.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  90.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  91.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92.    0xff, 0x3f, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  93.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  94.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  95.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  96.    0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
  97.    0x00, 0x80, 0x00, 0x00};
  98.  
  99. #define xhair32_width 32
  100. #define xhair32_height 32
  101. #define xhair32_x_hot 16
  102. #define xhair32_y_hot 15
  103. static char xhair32_bits[] = {
  104.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  105.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  106.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  107.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  108.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  109.    0xff, 0x7f, 0xfe, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0x00, 0x80, 0x01, 0x00,
  110.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  111.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  112.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  113.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00,
  114.    0x00, 0x80, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00};
  115.  
  116. #define xhair33_width 33
  117. #define xhair33_height 33
  118. #define xhair33_x_hot 16
  119. #define xhair33_y_hot 15
  120. static char xhair33_bits[] = {
  121.    0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  122.    0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  123.    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  124.    0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  125.    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  126.    0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127.    0x00, 0x00, 0x00, 0xff, 0x3f, 0xf8, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00,
  128.    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  129.    0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  130.    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  131.    0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  132.    0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  133.    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  134.    0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
  135.  
  136. #include <X11/bitmaps/xlogo32>
  137.  
  138. main()
  139. {
  140.   Display *dpy;
  141.   Window wind;
  142.   unsigned int scr;
  143.   unsigned int keep_going = 1;
  144.   XEvent ev;
  145.   unsigned int i, j;
  146.   const unsigned int MAX_curs = 8;
  147.   XColor backg, ebackg, foreg, eforeg;
  148.   XColor Red, eRed, Green, eGreen;
  149.   Pixmap xHairs[MAX_curs];
  150.   Cursor cHairs[MAX_curs];
  151.   Window rwin;
  152.   Colormap dcm;
  153.   GC dgc;
  154.   struct { unsigned char *bits; unsigned int w, h, hotx, hoty; } hairdefs[MAX_curs];
  155.   static unsigned char *id[] = {"0", "1", "2", "3", "4", "5", "6", "7"};
  156.   XWMHints    wmhints;
  157.   XSizeHints  wmsizehints;
  158.   XClassHint  classhint;
  159.  
  160.   hairdefs[0].bits = xhair15_bits;
  161.   hairdefs[0].w = xhair15_width;
  162.   hairdefs[0].h = xhair15_height;
  163.   hairdefs[0].hotx = xhair15_x_hot;
  164.   hairdefs[0].hoty = xhair15_y_hot;
  165.   
  166.   hairdefs[1].bits = xhair_bits;
  167.   hairdefs[1].w = xhair_width;
  168.   hairdefs[1].h = xhair_height;
  169.   hairdefs[1].hotx = xhair_x_hot;
  170.   hairdefs[1].hoty = xhair_y_hot;
  171.   
  172.   hairdefs[2].bits = xhair17_bits;
  173.   hairdefs[2].w = xhair17_width;
  174.   hairdefs[2].h = xhair17_height;
  175.   hairdefs[2].hotx = xhair17_x_hot;
  176.   hairdefs[2].hoty = xhair17_y_hot;
  177.   
  178.   hairdefs[3].bits = xhair30_bits;
  179.   hairdefs[3].w = xhair30_width;
  180.   hairdefs[3].h = xhair30_height;
  181.   hairdefs[3].hotx = xhair30_x_hot;
  182.   hairdefs[3].hoty = xhair30_y_hot;
  183.   
  184.   hairdefs[4].bits = xhair31_bits;
  185.   hairdefs[4].w = xhair31_width;
  186.   hairdefs[4].h = xhair31_height;
  187.   hairdefs[4].hotx = xhair31_x_hot;
  188.   hairdefs[4].hoty = xhair31_y_hot;
  189.   
  190.   hairdefs[5].bits = xhair32_bits;
  191.   hairdefs[5].w = xhair32_width;
  192.   hairdefs[5].h = xhair32_height;
  193.   hairdefs[5].hotx = xhair32_x_hot;
  194.   hairdefs[5].hoty = xhair32_y_hot;
  195.   
  196.   hairdefs[6].bits = xhair33_bits;
  197.   hairdefs[6].w = xhair33_width;
  198.   hairdefs[6].h = xhair33_height;
  199.   hairdefs[6].hotx = xhair33_x_hot;
  200.   hairdefs[6].hoty = xhair33_y_hot;
  201.   
  202.   hairdefs[7].bits = xlogo32_bits;
  203.   hairdefs[7].w = xlogo32_width;
  204.   hairdefs[7].h = xlogo32_height;
  205.   hairdefs[7].hotx = 10;
  206.   hairdefs[7].hoty = 10;
  207.   
  208.   dpy = XOpenDisplay("");
  209.   scr = DefaultScreen(dpy);
  210.   rwin = RootWindow(dpy, scr);
  211.   dcm = DefaultColormap(dpy, scr);
  212.   dgc = DefaultGC(dpy,scr);
  213.  
  214.   for( i = 0; i < MAX_curs; i++ ) {
  215.     xHairs[i] = XCreateBitmapFromData( dpy, rwin,
  216.                 hairdefs[i].bits, hairdefs[i].w, hairdefs[i].h);
  217.     if( xHairs[i] == NULL ) {
  218.       fprintf( stderr, "Error creating bitmap from data\n" );
  219.       exit(1);
  220.     }
  221.   }
  222.  
  223.   if( !XAllocNamedColor(dpy, dcm, "darkslategray", &backg, &ebackg)) {
  224.     fprintf( stderr, "Error getting darkslategray\n" ); exit(1); }
  225.  
  226.   if( !XAllocNamedColor(dpy, dcm,"ivory", &foreg, &eforeg)) {
  227.     fprintf( stderr, "Error getting Ivory\n" ); exit(1); }
  228.  
  229.   if( !XAllocNamedColor(dpy, dcm, "red", &Red, &eRed)) {
  230.     fprintf( stderr, "Error getting red\n" ); exit(1); }
  231.  
  232.   if( !XAllocNamedColor(dpy, dcm,"green", &Green, &eGreen)) {
  233.     fprintf( stderr, "Error getting green\n" ); exit(1); }
  234.  
  235.   for( i = 0; i < MAX_curs; i++ ) {
  236.     cHairs[i] = XCreatePixmapCursor(dpy, xHairs[i], xHairs[i], &Red, &Green,
  237.                    hairdefs[i].hotx, hairdefs[i].hoty);
  238.     if( cHairs[i] == NULL ) {
  239.       fprintf( stderr, "Error creating cursor from bitmap\n" );
  240.       exit(1);
  241.     }
  242.   }
  243.  
  244.   wind = XCreateSimpleWindow( dpy, rwin, 0, 0, 512, 512, 2,
  245.                               foreg.pixel, backg.pixel);
  246.   classhint.res_name = "LEFT or RIGHT Mouse to Change Cursor";
  247.   classhint.res_class = "GlyphCursor";
  248.   XSetClassHint(dpy, wind, &classhint);
  249.  
  250.   wmhints.input = True;
  251.   wmhints.flags = InputHint;
  252.   XSetWMHints(dpy, wind, &wmhints);
  253.  
  254.   wmsizehints.x = 100;
  255.   wmsizehints.y = 100;
  256.   wmsizehints.width = 100;
  257.   wmsizehints.height = 100;
  258.   wmsizehints.flags = USPosition | USSize;
  259.   XSetWMNormalHints(dpy, wind, &wmsizehints);
  260.  
  261.   XSetForeground(dpy, dgc, foreg.pixel);
  262.   XSelectInput( dpy, wind, ButtonPressMask|KeyPressMask|ExposureMask);
  263.   XMapWindow( dpy, wind );
  264.   
  265.   i = 0;
  266.   do { 
  267.     XNextEvent( dpy, &ev );
  268.     switch( ev.type )
  269.     {
  270.       case Expose:
  271.     XUndefineCursor( dpy, wind );
  272.     XClearWindow(dpy, wind);
  273.     XDefineCursor( dpy, wind, cHairs[i%MAX_curs] );
  274.     XDrawString(dpy, wind, dgc, 200, 200, id[i%MAX_curs], strlen(id[i%MAX_curs]));
  275.         break;
  276.  
  277.       case KeyPress:
  278.         XCloseDisplay(dpy);
  279.         keep_going = 0;
  280.         break;
  281.  
  282.       case ButtonPress:
  283.     if( ev.xbutton.button == Button1 )
  284.       i += 1;
  285.     else if( ev.xbutton.button == Button3 )
  286.       i -= 1;
  287.     else
  288.       i = 0;
  289.     XUndefineCursor( dpy, wind );
  290.     XClearWindow(dpy, wind);
  291.     XDefineCursor( dpy, wind, cHairs[i%MAX_curs] );
  292.     XDrawString(dpy, wind, dgc, 200, 200, id[i%MAX_curs], strlen(id[i%MAX_curs]));
  293.         break;
  294.  
  295.       default:
  296.         break;
  297.     }
  298.   } while( keep_going );
  299.   exit(0);
  300. }
  301.